Mongoose learning comprehension (recommended) and mongoose learning Comprehension
1. Create schemas
How to Create schemas:
var userSchema = new mongoose.Schema({ name: String, email: String, createdOn: Date });
Schemas has the following data types:
String
Number
Date
Boolean
Buffer
ObjectId
Mixed
Array
In particular, the ObjectId, Mixed, and Array types must be described. The follow
Mongoose simple table connection query and Mongoose simple
The original article is taken from my front-end blog. You are welcome to visit
Http://www.hacke2.cn
As I mentioned in this article, it is based on Node. js + jade + Mongoose mimic gokk. TV, at that time, the development was stopped because I deeply felt that I was wrong at that time. I should use two sche
Users collection:$ db.users.find ()Add another piece of data: (MongoDB is modeless, so you can add data in different formats)$ db.users.insert ({"username": "Zoe", "Group": "Reporter"})Then use Db.users.find () to view, as follows:To view the number of data:$ db.users.find (). Count ()query data by ID:$ db.users.find ({"_id": ObjectId ("584bc73ea635e489676cf5db")})Update data: (Match to the first article)$ db.users.update ({"username": "Zoe"}, {$set:
Mongoose getting started with mongooseNo matter what database you use as the db service, connect is an essential step. Method 1:
@ Param {String} uri @ param {Object} options @ param {Function} callbackconnect (uri, [options], [callbakc]) returns the mongoose Object. connect ('mongodb: // localhost/test', {mongos: true}, function (err, result) {if (err) {console. log ('error ing to MongoDB Database. '+ err)
Data model and Base operation templateIn order to make the engineering structure clear, the establishment of data Model (SCHEMA) and the base operation template of adding and deleting and modifying are named as collection in database design (corresponding to the table definition in relational database) and stored in the Models folder.Creation of schema and model:Schema is the data schema in mongoose, which can be understood as the table structure defi
}, sex: {type: String }}); // select the set var monModel = db. model ('user', monSchema); // condition var del = {name: "Nick"}; monModel. remove (del, function (err, result) {if (err) {console. log (err);} else {console. log ("update");} db. close ();});
Modify data
// Introduce the module var mongoose = require ('mongoose'); // connect to the database var db =
when calling its Delete method, because you will delete it from the database, not just the array.Populate existing documentIf we have an existing mongoose document and want to populate it with some path,mongoose >= 3.6 supports the Document#populate () method.Populating multiple existing documentIf we have one or more files, even simple objects (like the MapReduce output), we can populate them in
A burst of Byron teacher spoke about MongoDB's crud operations, including:How to create a new database (use dbname), delete a database (use dbname →db.dropdatabase ()), add data (db. CollectionName. Insert ({}) to view the data in the table (db. CollectionName. Find), sorted (sort), the data is too many to take the first few (. limit), skip the first few (. skip), limit and skip together in the time of the page is the most favorite, such as a page is 5, then. Limit (5). Skip (5*n).
mongoose. model.
After obtaining the Model object, you can perform operations such as adding, deleting, modifying, and querying. Model objects include methods such as find (), findOne (), update (), and remove (), which are similar to the usage in mongo shell. Each of these methods has an optional callback. When you provide these callback, the execution result will be returned to you through this callback.
Database
var db = mongoose.createconnection (' mongodb://127.0.0.1:27017/test ');
Set data type
var Monschema = new Mongooose. Schema ({
name:{type:string,default: "username"},
Age:{type:number},
sex:{type:string}
);
Select Set
var Monmodel = Db.model (' user ', Monschema);
DataSet
var content = {Name: "Nick", Age:23,sex: ' Male '};
Instantiate the object and insert the data
var moninsert = new Monmodel (content);
Moninsert.save (function (err) {
if (err) {
console.log (err);
} else{
con
when defining a schema is consistent with the first parameter of the Mongoose.model.
Get the Model object, you can perform additions and deletions to check and other operations. The model object has methods such as find (), FindOne (), update (), remove (), and so on, similar to our usage in the MONGO shell. These methods have an optional callback, and when you provide these callback, the results of the execution will be returned to you through this
Using mongoose allows us to better use the MongoDB database without having to write tedious business logic.
Installation
NPM Install Mongoose
Initialize the use ofbefore using mongoose, you need to install node and MongoDB, which do not talk about node and MongoDB installation methods.
var mongoose
Mongoose Library in short, a convenient package for manipulating MongoDB databases in the node environment, an object model tool similar to Orm,mongoose converts data from a database into JavaScript objects for you to use in your app.Of course, you have to install the Environment node. js and MongoDB = "MongoDB installationMongoDB is one of the most popular NoSQL databases, developed specifically for node.
to these events. In our sample code, I listen to the Open event, in the callback function, define the schema, call Mongoose.model to compile the schema to get the model object. It is important to note that the collection name specified when the schema is defined is consistent with the first parameter of the Mongoose.model. Get the Model object, you can perform additions and deletions and other operations. The model object has methods such as find (), FindOne (),
sort sort 1 ascending-1 descending execution will first sort and then skip, then Limitpersonmodel.find ({},{_id:0,name:1 },{limit:3,skip:3,sort:{age:1,name:-1}},function (err,docs) { console.log (docs);});• Save-model provides a create method to save the data. Model.create (document data, callback)Insert 10 documents into the collection for (Var i=1;i-entity provides a save method for saving data. Entity.save (document data, callback)Creates an entity from a model, refers to the individual o
Mongoose Reference Manualtags (space delimited): MongoDB
In general, we do not directly use MongoDB functions to manipulate MongoDB database Mongose is a set of Operations MongoDB database interface.SchemaA database model skeleton, stored as a file, does not have direct access to the database, which means that it does not have the ability to manipulate the database. Can be said to be a data attribute model (the traditional meaning of the table s
according to a field: coll.find({name:"ZhangSan"})
Remove a document from the collection Use the Remove () method of the collection object to delete the document. Remove () without parameters deletes all documents in the collection. Remove () with parameters removes only the document specified by the parameter. such as the following: use accounts
coll = db.getCollection("accounts")
coll.insert({name:"QianQi",password:"888888"})
coll.find()
coll.remove({name:"WangEr"})
coll.find()
coll.remove()
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.